﻿using System;
using visinia.SmartEngine.Installer;

namespace visinia.web.admin.pages
{
    public partial class AdminEditPlugin : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["PluginId"] != null)
            {
                int pluginid = 0;
                if (Int32.TryParse(Request.QueryString["PluginId"], out pluginid))
                {
                    Plugin plugin = PluginController.GetInstalledPlugin(pluginid);
                    if (plugin != null)
                    {
                        UrlSeparator.Text = plugin.UrlSeparator;
                    }
                }
            }
        }

        protected void UPDATE_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["PluginId"] != null)
            {
                int pluginid = 0;
                if (Int32.TryParse(Request.QueryString["PluginId"], out pluginid))
                {
                    Plugin plugin = PluginController.GetInstalledPlugin(pluginid);
                    if (plugin != null)
                    {
                        if (PluginController.GetInstalledPlugins().Exists(p => string.Compare(p.UrlSeparator, UrlSeparator.Text, true) == 0))
                        {
                            Message.InnerText = "Url Separator occupied use another one (two plugin can't use the same url separator)";
                            Message.Visible = true;
                        }
                        else
                        {
                            plugin.UrlSeparator = UrlSeparator.Text;
                            PluginController.UpdatePlugin(plugin);
                            Message.InnerText = "Update Complete";
                            Message.Visible = true;
                        }
                    }
                }
            }
        }

        protected void CANCEL_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }
    }
}